<?php
function getip() { 
if (isset($_SERVER)) { 
/*在局域网通过透明代理访问外部的web服务器时,
*在web服务器端,
*通过header HTTP_X_FORWARDED_FOR 可以知道代理服务器的服务器名以及端口,
*/ 
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { 
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
} elseif (isset($_SERVER["HTTP_CLIENT_IP"])) { 
$realip = $_SERVER["HTTP_CLIENT_IP"]; 
} else { 
$realip = $_SERVER["REMOTE_ADDR"]; 
} 
} else { 
/*string getenv ( string varname )*Returns the value of the environment variable varname, or FALSE on *an error. 

*/ 

if (getenv("HTTP_X_FORWARDED_FOR")) { 
$realip = getenv("HTTP_X_FORWARDED_FOR"); 
} elseif (getenv("HTTP_CLIENT_IP")) { 
$realip = getenv("HTTP_CLIENT_IP"); 
} else { 
$realip = getenv("REMOTE_ADDR"); 
} 
} 
return $realip; 
}

function convertWk($wk){
	$wk=str_replace("1","一",$wk);
	$wk=str_replace("2","二",$wk);
	$wk=str_replace("3","三",$wk);
	$wk=str_replace("4","四",$wk);
	$wk=str_replace("5","五",$wk);
	$wk=str_replace("6","六",$wk);
	$wk=str_replace("0","日",$wk);	 
	return $wk;
}
function showColor($i)
{
	if ($i%2==0)
		$showColor = "#99CC00";
	else if ($i%3==0)
		$showColor="#FF00FF";
	else
		$showColor="#CC99FF";
	return $showColor;
}
function head()
{
	echo "<meta http-equiv=\"Content-Language\" content=\"zh-cn\">\r\n";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n";
	echo "<meta http-equiv=\"author\" content=\"\">\r\n";
	echo "<meta http-equiv=\"keyword\" content=\"\">\r\n";
	echo "<meta http-equiv=\"description\" content=\"\">\r\n";
	echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\">\r\n";
}
function successmsgbox()
{
	echo "<script language='javascript' type='text/javascript'>alert('登陆成功');</script>";
}
function failmsgbox($msg)
{
	echo "<script language='javascript' type='text/javascript'>alert('".$msg."');location.href='login.php';</script>";
}
function logout()
{
	$_SESSION["admin"]="";
	redirect('login.php');	
}
function redirect($url)
{
	die("<script language='javascript' type='text/javascript'>location.href='".$url."';</script>");
}
function check()
{
	
	if(!isset($_SESSION["admin"]) || $_SESSION["admin"]=="")
	{
		echo "<script language='javascript' type='text/javascript'>alert('登陆失败或超时,请重新登陆!');
		      if(!window.top)		          
		      {
				  location.href='login.php';
	          }
			  else
		      {
				  window.top.location.href='login.php';
	          }
		      </script>";
	}
}
function successmsg($msg)
{
	echo "<html>\r\n";
	echo "<head>\r\n";
	head();
	echo "<title>投票系统</title>\r\n";
	echo "</head>\r\n";
	echo "<body>\r\n"; 
	echo "<table width=\"40%\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\" bgcolor=\"#BBDDFF\" id=\"table2\" style=\"border-collapse: collapse; margin-top:50px;\">\r\n";
	echo "<tr>\r\n";
	echo "<td bgcolor=\"#ECF5FF\" align=\"center\" height=\"100\">\r\n";
	echo "<label id=\"errmsg\" name=\"errmsg\">".$msg."</label><br><br>\r\n";
	echo "<a href=\"".$_SERVER["HTTP_REFERER"]."\">点此返回再次操作</a>&nbsp;&nbsp;<a href=\"admin_index.php\" target=\"_top\">进入管理首页</a>\r\n";    //返回时会自动刷新页面
	echo "</td>\r\n";
	echo "</tr>\r\n";
	echo "</body>\r\n";
	echo "</html>\r\n";	
}
function errormsg($msg)
{
	echo "<html>\r\n";
	echo "<head>\r\n";
	head();
	echo "<title>投票系统</title>\r\n";
	echo "</head>\r\n";
	echo "<body>\r\n";
	echo "<table width=\"40%\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\" bgcolor=\"#BBDDFF\" id=\"table2\" style=\"border-collapse: collapse; margin-top:50px;\">\r\n";
	echo "<tr>\r\n";
	echo "<td bgcolor=\"#ECF5FF\" align=\"center\" height=\"100\">\r\n";
    echo "<label id=\"errmsg\" name=\"errmsg\">".$msg."</label><br><br>\r\n";
	echo "<a href=\"".$_SERVER["HTTP_REFERER"]."\">点此返回</a>"; //返回时会自动刷新页面
	echo "</td>\r\n";
	echo "</tr>\r\n";
	echo "</body>\r\n";
	echo "</html>\r\n";	
}
function pager($recordcount,$pagesize,$curpage,$pages,$key,$url)
{//$recordcount 记录总数
 //$pagesize 每页记录数
 //$curpage 当前页面号
 //$pages 要显示多少个页面 $pages=1 就显示两个页面 $pages=2就显示三个页面
 //$key 关键字值
 //$url 分页链接到哪个页面
 global $outhtml;
 global $firstcount;
 $totalpage = max(ceil($recordcount/$pagesize),1);
 if($curpage<0 || $curpage>$totalpage) $curpage=1;
 $outhtml = "共&nbsp;".$recordcount."&nbsp;条记录&nbsp第&nbsp;".$curpage."&nbsp页/共&nbsp;".$totalpage."&nbsp;页&nbsp;每页&nbsp;".$pagesize."&nbsp;条记录&nbsp;";
 $pageno = $curpage;
 if($pageno<0 || $pageno>$totalpage) $pageno=1;
 $firstcount=$pagesize*($pageno-1);
 $outhtml.="<a href='$url?page=1&k=$key'><<</a>&nbsp;";
 //$showpage = min($pages,$totalpage); //要显示的页面号
 if($pages>$totalpage)
 {
      $startpage=1;
	  $endpage=min($startpage+$pages,$totalpage);
 }
 else
 {
	 $startpage=max($totalpage-$pages,1);
	 $startpage=min($startpage,$curpage);
	 $endpage=min($startpage+$pages,$totalpage);
 }
 if($pageno>1)
 {
	 $outhtml.="<a href='$url?page=".($pageno-1)."&k=$key'><</a>&nbsp;";
 }
 else
 {
	 $outhtml.="<a href='$url?page=1&k=$key'><</a>&nbsp;";
 }
 for($i=$startpage;$i<=$endpage;$i++)
 {
	 if($curpage==$i)
	 {
		 $outhtml.="<font color='#FF0000'>$i</font>&nbsp;";
	 }
	 else
	 {
		 $outhtml.="<a href='$url?page=$i&k=$key'>$i</a>&nbsp;";
	 }
 }
 if($pageno<$totalpage)
 {
	 $outhtml.="<a href='$url?page=".($pageno+1)."&k=$key'>></a>&nbsp;";
 }
 else
 {
	 $outhtml.="<a href='$url?page=$totalpage&k=$key'>></a>&nbsp;";
 }
 $outhtml.="<a href='$url?page=$totalpage&k=$key'>>></a>&nbsp;"; 
}
//格式化日期
function mkDate($v) {
	do {
		if (empty ($v))
			break;

		$ymdhis = explode(' ', trim($v));

		if (count($ymdhis) < 1)
			break;

		$ymd = explode('-', $ymdhis[0]);

		$his = explode(':', $ymdhis[1]);

		return mktime($his[0], $his[1], $his[2], $ymd[1], $ymd[2], $ymd[0]);
	} while (false);

	return time();
}
function deldir($dir) {
  $dh=@opendir($dir);
  while ($file=@readdir($dh)) {
    if($file!="." && $file!="..") {
      $fullpath=$dir."/".$file;
      if(!is_dir($fullpath)) {
          @unlink($fullpath);
      } else {
          deldir($fullpath);
      }
    }
  }

  @closedir($dh);
  
  if(@rmdir($dir)) {
    return true;
  } else {
    return false;
  }
}
?>